#!/bin/sh

updBinFiles ()
{
  OLDVAR=$1
  NEWVAR=$2
  BINDIR=$3
  #mkdir $BINDIR
  grep -l $OLDVAR $BINDIR/* | while read FILE
  do
    echo "Updating $FILE"
    cp $FILE $FILE.old
    cat $FILE.old | sed s/\$$OLDVAR/\$$NEWVAR/g > $FILE
    chmod a+rx $FILE
  done
}


OLDVAR=$1
NEWVAR=$2
if [ "$NEWVAR" = "" ]; then
  echo "$0 oldvar newvar"
  exit
else
  echo "Replacing $OLDVAR with $NEWVAR."
  INFILE=/etc/sipman.conf
  echo "updating $INFILE"
  cp $INFILE $INFILE.old
 # cat $INFILE | sed 's/$OLDVAR/$NEWVAR/g' > $INFILE.new
  cat $INFILE.old | sed s/$OLDVAR/$NEWVAR/g > $INFILE
  for DIR in /var/sipman3/main/bin /var/sipman3/main/plugins /var/sipman3/main/cgi-bin
  do
    echo "Processing dir $DIR"
    updBinFiles $OLDVAR $NEWVAR $DIR
  done
fi
